package com.ykdl.member.kid.messagebox;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import oauth.signpost.http.HttpParameters;
import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.adapters.MessageAdapter1;
import com.ykdl.member.kid.adapters.MyReanswerListAdapter;
import com.ykdl.member.kid.adapters.OfficialMessageAdapter1;
import com.ykdl.member.kid.beans.OfficialMessageListBean;
import com.ykdl.member.kid.beans.PraiseListBean;
import com.ykdl.member.kid.beans.ResetPasswordBean;
import com.ykdl.member.kid.beans.ResultBean;
import com.ykdl.member.kid.beans.SystemMessageBean;
import com.ykdl.member.kid.beans.SystemMessageExtraContextBean;
import com.ykdl.member.kid.beans.SystemMessageListBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.messagebox.MyMessageAcivity1.getSystemMessageTag;
import com.ykdl.member.kid.messagebox.PraisesMeListActivity.getPraisesTag;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.kid.widget.RefreshListView.OnRefreshListener;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class OfficialMessageActivity1 extends BaseScreen implements OnClickListener{
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		setResult(100);
		super.onBackPressed();
		
	}
	private OfficialMessageAdapter1 messageadapter;
	private RefreshListView officialmessage_list;
	private RelativeLayout relative;
	 private boolean isMulChoice=false;
	 private ArrayList<SystemMessageExtraContextBean> selectid;
	 private String message_id,title_name;
	 private Button all_bt ;
	 private int start_id = 0;
	 private int cursor = 0;
	 private int count = 40;
	 private ArrayList<SystemMessageExtraContextBean>  list = new ArrayList<SystemMessageExtraContextBean>(); 
	 private boolean issetleftlist= false;
	 private OfficialMessageListBean bean;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("官方消息", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				setResult(100);
				finish();
			}
		}, R.string.edit_baby, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				int index = messageadapter.getCount();
				if (View.GONE==relative.getVisibility()) {
					relative.setVisibility(View.VISIBLE);
					setRightNav("取消");
					for(int i=0;i<index;i++){
						messageadapter.visibleMap.put(i, CheckBox.VISIBLE);
						messageadapter.notifyDataSetChanged();
					}
				}else {
					setRightNav("编辑");
					relative.setVisibility(View.GONE);
					for(int i=0;i<index;i++){
						messageadapter.visibleMap.put(i, CheckBox.GONE);
						messageadapter.notifyDataSetChanged();
					}
				}
//	                selectid.clear();
			}
		}, R.drawable.title_bg, false);
		message_id = this.getIntent().getStringExtra(KidAction.MESSAGE_ID);
		title_name = this.getIntent().getStringExtra(KidAction.TITLE_NAME);
		setContent(R.layout.officialmessageactivity1_xml);
		inint();
		}
	public void inint() {
		all_bt =(Button)findViewById(R.id.all_bt);
		findViewById(R.id.all_bt).setOnClickListener(this);
		findViewById(R.id.delete_bt).setOnClickListener(this);
		relative = (RelativeLayout)findViewById(R.id.relative);
		messageadapter = new OfficialMessageAdapter1(this);
		officialmessage_list = (RefreshListView)findViewById(R.id.officialmessage_list);
		officialmessage_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				   if(View.VISIBLE==relative.getVisibility()){
					SystemMessageExtraContextBean bean = (SystemMessageExtraContextBean)messageadapter.getItem(arg2);
					if (messageadapter.checkedMap.get(arg2-1)) {
						messageadapter.getChecked_message_id().remove(bean.getMessage_id());
						messageadapter.checkedMap.put(arg2-1, false);
					}else {
						if (!messageadapter.getChecked_message_id().contains(bean.getMessage_id())) {
							messageadapter.getChecked_message_id().add(bean.getMessage_id());
						}
						messageadapter.checkedMap.put(arg2-1, true);
					}
				
					String delt_idString = messageadapter.getChecked_message_id().toString().replace("[", "").replace("]", "");
		        	Log.e("alan", "---1---delt_idString:"+delt_idString);
					messageadapter.notifyDataSetChanged();
					if(messageadapter.getChecked_message_id().size()==messageadapter.getCount()){
						all_bt.setText("取消全选");
					}else{
						all_bt.setText("全选");
					}
				 }
				   
			}
		});
		officialmessage_list.setonRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				if(View.VISIBLE==relative.getVisibility()){
					officialmessage_list.onRefreshComplete();
					officialmessage_list.setFecthMoreOk();
					return;
				}
				issetleftlist = false;
				count = 20;
				cursor = 0;
				list=new ArrayList<SystemMessageExtraContextBean>();
				getOfficialmessages(true);
			}
			});
		officialmessage_list.setMoreClick(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(View.VISIBLE==relative.getVisibility()){
					officialmessage_list.onRefreshComplete();
					officialmessage_list.setFecthMoreOk();
					return;
				}
				issetleftlist = true;
				getOfficialmessages(false);
			}});
		getOfficialmessages(true);
	}
	public void deltOfficialmessages(String message_id_list) {
		showProgress("正在加载中,请稍等...");
		String  pageurl =KidConfig.DELETE_NEWS;
		HttpParameters parameterss = new HttpParameters();
		parameterss.put("delete_type", "single");
		parameterss.put("messages", message_id_list);
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl, parameterss,"");
		 TaskManager.startHttpRequest(request, new deltOfficialmessages(),  ResetPasswordBean.class);
	}
	
	class deltOfficialmessages implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ResetPasswordBean) {
				ResetPasswordBean bean = (ResetPasswordBean)t;
				if (null==bean.getError()) {
					Toast.makeText(mContext, "删除成功", 1).show();
					messageadapter.clearList();
					setRightNav("编辑");
					relative.setVisibility(View.GONE);
					int index = messageadapter.getCount();
					for(int i=0;i<index;i++){
						messageadapter.visibleMap.put(i, CheckBox.GONE);
						messageadapter.notifyDataSetChanged();
					}
					getOfficialmessages(false);
				}else {
					Toast.makeText(mContext, "删除失败,"+bean.getDesc(), 1).show();
				}
			}
			
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void getOfficialmessages(boolean isshow) {
		if (isshow) {
			showProgress("正在加载中,请稍等...");
		}
//		relative.setVisibility(View.GONE);
		String  pageurl =KidConfig.READ_NEWS+message_id;
		HttpParameters parameterss = new HttpParameters();
		parameterss.put("start_id", String.valueOf(start_id));
		parameterss.put("cursor", String.valueOf(cursor));
		parameterss.put("count", String.valueOf(count));
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl, parameterss,"");
		 TaskManager.startHttpRequest(request, new getOfficialmessagesTag(isshow),  OfficialMessageListBean.class);
	}
	
	class getOfficialmessagesTag implements ITag {
		private boolean isshow;
		public getOfficialmessagesTag(boolean isshow){
			this.isshow=isshow;
		}
		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载成功", 1).show();
			if (t instanceof OfficialMessageListBean) {
			  bean = (OfficialMessageListBean)t;
				if(null==bean.getError()){
					int index = 0;
					if (bean.getList().size() > 1) {
						index = bean.getList().size() - 1;  
					}
					if (bean.getList().size()>0) {
						setTopNav(bean.getList().get(0).getFrom_actor().getDisplay_name());
						if (isshow) {
							for (SystemMessageExtraContextBean obj : bean.getList()) {
								list.add(obj);
							}
							messageadapter.setlist(list);
						}else {
							messageadapter.setlist(bean.getList());
						}
						
						officialmessage_list.setVisibility(View.VISIBLE);
						officialmessage_list.setAdapter(messageadapter);
						messageadapter.notifyDataSetChanged();
						hidErrorTip( );
						if (bean.getNext_cursor() == bean.getTotal_number()) {
							officialmessage_list.setMoreButtoIsGon(true);
						} else {
							cursor = bean.getNext_cursor();
							officialmessage_list.setMoreButtoIsGon(false);
						}
					}else{
						officialmessage_list.setVisibility(View.GONE);
						showErrorTip("没有数据","");
					}
					if (issetleftlist) {
						officialmessage_list.setSelection(index);
					}
					officialmessage_list.onRefreshComplete();
					officialmessage_list.setFecthMoreOk();

				}else{
					Toast.makeText(mContext, "失败,"+bean.getDesc(), 1).show();
				}
			}
			
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	protected void onResume() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		 switch (v.getId()) {
	        case R.id.all_bt:
	        	int index = messageadapter.getCount();
	        	if (!isMulChoice) {
	        		all_bt.setText("取消全选");
	        		messageadapter.getChecked_message_id().clear();
	        		for(int i=0;i<index;i++){
						messageadapter.checkedMap.put(i, true);
						messageadapter.checked_message_id.add(bean.getList().get(i).getMessage_id());
						messageadapter.notifyDataSetChanged();
						isMulChoice=true;
					}
				}else {
					all_bt.setText("全选");
					for(int i=0;i<index;i++){
						messageadapter.checkedMap.put(i, false);
						messageadapter.notifyDataSetChanged();
						isMulChoice=false;
					}
					messageadapter.getChecked_message_id().clear();
				}
	            break;
	        case R.id.delete_bt:
	        	String delt_idString = messageadapter.getChecked_message_id().toString().replace("[", "").replace("]", "");
	        	Log.e("alan", "------delt_idString:"+delt_idString);
	        	if (!delt_idString.equals("")) {
	        		deltOfficialmessages(delt_idString);
				}else {
					Toast.makeText(mContext, "请选择", 1).show();
				}
	        	
	            break;
	        default:
	            break;
	        }
	}
}